home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / langs / xlisp2.1 / xldist02.zoo / sources / xlio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-09  |  4.4 KB  |  235 lines

  1. /* xlio - xlisp i/o routines */
  2. /*        Copyright (c) 1985, by David Michael Betz
  3.         All Rights Reserved
  4.         Permission is granted for unrestricted non-commercial use        */
  5.  
  6. #include "xlisp.h"
  7.  
  8. /* external variables */
  9. extern LVAL s_stdout,s_stderr,s_debugio,s_traceout;
  10. extern int xlfsize;
  11.  
  12. /* xlgetc - get a character from a file or stream */
  13. int xlgetc(fptr)
  14.   LVAL fptr;
  15. {
  16.     LVAL lptr,cptr;
  17.     FILE *fp;
  18.     int ch;
  19.  
  20.     /* check for input from nil */
  21.     if (fptr == NIL)
  22.         ch = EOF;
  23.  
  24.     /* otherwise, check for input from a stream */
  25.     else if (ustreamp(fptr)) {
  26.         if ((lptr = gethead(fptr)) == NIL)
  27.             ch = EOF;
  28.         else {
  29.             if (!consp(lptr) || (cptr = car(lptr)) == NIL || !charp(cptr))
  30.                 xlfail("bad stream");
  31.             sethead(fptr,lptr = cdr(lptr));
  32.             if (lptr == NIL)
  33.                 settail(fptr,NIL);
  34.             ch = getchcode(cptr);
  35.         }
  36.     }
  37.  
  38.     /* otherwise, check for a buffered character */
  39.     else if ((ch = getsavech(fptr)) != 0)
  40.         setsavech(fptr,'\0');
  41.  
  42.     /* otherwise, check for terminal input or file input */
  43.     else {
  44.         fp = getfile(fptr);
  45.         if (fp == stdin || fp == stderr)
  46.             ch = ostgetc();
  47.         else {
  48. #ifdef BETTERIO
  49.             if ((fptr->n_sflags & S_READING) == 0) { 
  50.                 /* possible direction change*/
  51.                 if (fptr->n_sflags & S_WRITING) {
  52.                     fseek(fp,0L,SEEK_CUR);
  53.                 }
  54.                 fptr->n_sflags = S_READING;
  55.             }
  56. #endif
  57.             ch = fgetc(fp);
  58.         }
  59.     }
  60.  
  61.     /* return the character */
  62.     return (ch);
  63. }
  64.  
  65. /* xlungetc - unget a character */
  66. VOID xlungetc(fptr,ch)
  67.   LVAL fptr; int ch;
  68. {
  69.     LVAL lptr;
  70.     
  71.     /* check for ungetc from nil */
  72.     if (fptr == NIL)
  73.         ;
  74.         
  75.     /* otherwise, check for ungetc to a stream */
  76.     else if (ustreamp(fptr)) {
  77.         if (ch != EOF) {
  78.             lptr = cons(cvchar(ch),gethead(fptr));
  79.             if (gethead(fptr) == NIL)
  80.                 settail(fptr,lptr);
  81.             sethead(fptr,lptr);
  82.         }
  83.     }
  84.     
  85.     /* otherwise, it must be a file */
  86.     else
  87.         setsavech(fptr,ch);
  88. }
  89.  
  90. /* xlpeek - peek at a character from a file or stream */
  91. int xlpeek(fptr)
  92.   LVAL fptr;
  93. {
  94.     LVAL lptr,cptr;
  95.     int ch;
  96.  
  97.     /* check for input from nil */
  98.     if (fptr == NIL)
  99.         ch = EOF;
  100.  
  101.     /* otherwise, check for input from a stream */
  102.     else if (ustreamp(fptr)) {
  103.         if ((lptr = gethead(fptr)) == NIL)
  104.             ch = EOF;
  105.         else {
  106.             if (!consp(lptr) || (cptr = car(lptr)) == NIL || !charp(cptr))
  107.                 xlfail("bad stream");
  108.             ch = getchcode(cptr);
  109.         }
  110.     }
  111.  
  112.     /* otherwise, get the next file character and save it */
  113.     else {
  114.         ch = xlgetc(fptr);
  115.         setsavech(fptr,ch);
  116.     }
  117.  
  118.     /* return the character */
  119.     return (ch);
  120. }
  121.  
  122. /* xlputc - put a character to a file or stream */
  123. VOID xlputc(fptr,ch)
  124.   LVAL fptr; int ch;
  125. {
  126.     LVAL lptr;
  127.     FILE *fp;
  128.  
  129.     /* count the character */
  130.     ++xlfsize;
  131.  
  132.     /* check for output to nil */
  133.     if (fptr == NIL)
  134.         ;
  135.  
  136.     /* otherwise, check for output to an unnamed stream */
  137.     else if (ustreamp(fptr)) {
  138.         lptr = consa(cvchar(ch));
  139.         if (gettail(fptr))
  140.             rplacd(gettail(fptr),lptr);
  141.         else
  142.             sethead(fptr,lptr);
  143.         settail(fptr,lptr);
  144.     }
  145.  
  146.     /* otherwise, check for terminal output or file output */
  147.     else {
  148.         fp = getfile(fptr);
  149.         if (fp == stdout || fp == stderr)
  150.             ostputc(ch);
  151.         else {
  152. #ifdef BETTERIO
  153.             if ((fptr->n_sflags & S_WRITING) == 0) { 
  154.                 /* possible direction change*/
  155.                 if (fptr->n_sflags & S_READING) {
  156.                     fseek(fp,
  157.                         (getsavech(fptr)?(setsavech(fptr,'\0'),-1L):0L),
  158.                         SEEK_CUR);
  159.                 }
  160.                 fptr->n_sflags = S_WRITING;
  161.             }
  162. #endif
  163.             if (fputc(ch,fp)==EOF)    /* TAA MOD to check for write to RO file*/
  164.                 xlfail("write failed");
  165.         }
  166.     }
  167. }
  168.  
  169. /* xlflush - flush the input buffer */
  170. VOID xlflush()
  171. {
  172.     osflush();
  173. }
  174.  
  175. /* stdprint - print to *standard-output* */
  176. VOID stdprint(expr)
  177.   LVAL expr;
  178. {
  179.     xlprint(getvalue(s_stdout),expr,TRUE);
  180.     xlterpri(getvalue(s_stdout));
  181. }
  182.  
  183. /* stdputstr - print a string to *standard-output* */
  184. VOID stdputstr(str)
  185.   char *str;
  186. {
  187.     xlputstr(getvalue(s_stdout),str);
  188. }
  189.  
  190. /* errprint - print to *error-output* */
  191. VOID errprint(expr)
  192.   LVAL expr;
  193. {
  194.     xlprint(getvalue(s_stderr),expr,TRUE);
  195.     xlterpri(getvalue(s_stderr));
  196. }
  197.  
  198. /* errputstr - print a string to *error-output* */
  199. VOID errputstr(str)
  200.   char *str;
  201. {
  202.     xlputstr(getvalue(s_stderr),str);
  203. }
  204.  
  205. /* dbgprint - print to *debug-io* */
  206. VOID dbgprint(expr)
  207.   LVAL expr;
  208. {
  209.     xlprint(getvalue(s_debugio),expr,TRUE);
  210.     xlterpri(getvalue(s_debugio));
  211. }
  212.  
  213. /* dbgputstr - print a string to *debug-io* */
  214. VOID dbgputstr(str)
  215.   char *str;
  216. {
  217.     xlputstr(getvalue(s_debugio),str);
  218. }
  219.  
  220. /* trcprin1 - print to *trace-output* */
  221. VOID trcprin1(expr)
  222.   LVAL expr;
  223. {
  224.     xlprint(getvalue(s_traceout),expr,TRUE);
  225. }
  226.  
  227. /* trcputstr - print a string to *trace-output* */
  228. VOID trcputstr(str)
  229.   char *str;
  230. {
  231.     xlputstr(getvalue(s_traceout),str);
  232. }
  233.  
  234.  
  235.